package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author zhouwei
 * @date 2020-01-06 14:28
 */
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;

    /**
     * 预约后台处理
     * @param map
     * @return
     */
    @Override
    public Result order(Map map) throws Exception {
        //校验所选日期是否已被设置了预约
        String orderDate = (String) map.get("orderDate");
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(DateUtils.parseString2Date(orderDate));
        if (orderSetting == null) {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        //校验预约日期人数是否已满
        if (orderSetting.getNumber() == orderSetting.getReservations()) {
            return new Result(false, MessageConstant.ORDER_FULL);
        }

        //校验是否重复预约，同一人同一天同一套餐(会员id，预约日期，套餐id)
        Member member = memberDao.findByTelephone((String) map.get("telephone"));
        //注册过，已是就会员
        if (member != null) {
            Integer memberId = member.getId(); //会员id
            Date date = DateUtils.parseString2Date(orderDate);//预约时间
            String setmealId = (String) map.get("setmealId");//套餐id
            Order order = new Order();
            order.setMemberId(memberId);
            order.setOrderDate(date);
            order.setSetmealId(Integer.parseInt(setmealId));
            //查询order是否存在
            List<Order> list = orderDao.findByCondition(order);
            if (list != null && list.size() > 0) {
                return new Result(false, MessageConstant.HAS_ORDERED);
            }
        } else {
            //非会员，自动完成注册
             member = new Member();
            member.setPhoneNumber((String) map.get("telephone"));
            member.setName((String) map.get("name"));
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());
            memberDao.add(member);
        }

            //保存预约信息
            Order order = new Order();
            Integer memberId = member.getId(); //会员id
            //预约时间
            Date orderDate1 = DateUtils.parseString2Date(orderDate);
            String setmealId = (String) map.get("setmealId");//套餐id
            Integer id = member.getId();
            String orderType = (String) map.get("orderType");//预约方式
            order.setMemberId(memberId);
            order.setOrderDate(orderDate1);
            order.setSetmealId(Integer.parseInt(setmealId));
            order.setOrderType(orderType);
            orderDao.add(order);


            //更新预约人数
            orderSetting.setReservations(orderSetting.getReservations() + 1);
            orderSettingDao.updataOrderSettigByDate(orderSetting);
            return new Result(true, MessageConstant.ORDER_SUCCESS, order);
        }


    /**
     * 查询预约信息
     * @param id
     * @return
     */
    @Override
    public Map findById(Integer id) {
        Map map=orderDao.findById(id);
        return map;
    }
}
